package com.xjzd.spring.converter;

import com.xjzd.spring.validated.Address;
import com.xjzd.spring.validated.User;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;

public class String2UserConverter implements Converter<String, User>, ConditionalConverter {

    @Override
    public User convert(String source) {
        User user = new User();
        user.setName(source);
        user.setAge(18);
        user.setAddress(new Address());
        return user;
    }

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        return targetType.getType().equals(User.class);
    }
}
